new Vue({
    el: '#app',
    data() {
        return {
            socketTask: null,//websocket服务器
            is_open_socket: false,//判断websocket服务器是否开启
            user: [

            ],
            //存放用户信息和与用户聊天记录
            masseg: '',//发送的消息
            user2: [
            ],
            isShow: false,
            index: 0
        }
    },
    mounted() {
        this.getList(); //获取历史记录
    },
    methods: {
        //获取头像
        cs(obj1) {//重整数组
            // 检查 user 数组中是否存在 obj1.opid
            const _this = this;
            const existingUser = this.user.find(user => user.Open_ID === obj1.Open_ID);
            if (!existingUser) {
                // 如果不存在，则添加新的用户对象到 user 数组
                $.ajax({
                    url: DomainName + '/getInquiryIDAction?token=' + $.cookie("token"),
                    data: {
                        Primary_Key_Name: "OPen_ID",
                        Primary_Key: obj1.Open_ID,
                        Data: "member_info",
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    },
                    success: function (Result) {
                        const newUser = {
                            Open_ID: obj1.Open_ID,
                            list: [],
                            Seed_Message: "",
                            Image: Result[0].Image,
                            Nick: Result[0].Nick,
                            Enterprise_Info_SerialNumber: Result[0].Enterprise_Info_SerialNumber,
                            Member_SerialNumber: Result[0].Member_SerialNumber,
                            App_ID: Result[0].App_ID
                        };

                        newUser.list.push({
                            Message: obj1.Message,
                            Time: obj1.Time,
                            type: 0
                        });

                        _this.user.push(newUser);
                    },
                    dataType: "json"
                });
                console.log(this.user);
            } else {
                // 如果存在，则直接将消息对象添加到对应用户的 list 数组中
                existingUser.list.push({
                    Message: obj1.Message,
                    Time: obj1.Time,
                    type: 0
                });
            }
        },
        handle(index) {//点击联系人
            this.user2 = this.user[index].list;
            this.isShow = true
            this.index = index;
        },
        isArrNull(index, arr) {
            let num = 0;
            arr.forEach(item => {
                if (item.Open_ID == index) {
                    num++
                }
            })
            if (num == 0) {
                return true
            } else {
                return false
            }

        },
        //

        getList() {     //换页方法
            const _this = this
            $.ajax({
                type: 'POST',
                url: DomainName + '/WXMemberServes',
                data: {
                    User_Type: "service",
                    userid: $.cookie("use"),
                    Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    console.log(Result);
                    _this.user = Result.Data
                },
                dataType: "json"
            });
        },
    },
})